package com.lxl.xm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxl.xm.common.BaseResponse;
import com.lxl.xm.common.ResultUtils;
import com.lxl.xm.mapper.AdContentMapper;
import com.lxl.xm.model.entity.AdContent;
import com.lxl.xm.model.entity.Advertiser;
import com.lxl.xm.service.AdContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adcontent")
@Slf4j
public class AdContentController {
    @Resource
    AdContentService adContentService;

    /**
     *
     * 广告内容分页查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/page")
    public BaseResponse<Page<AdContent>> getAdContents(@RequestParam(name = "page",defaultValue = "1")int current,
                                                       @RequestParam(name = "size",defaultValue = "10")int size){
        Page<AdContent> page = adContentService.getAdContentPage(current,size);
        return ResultUtils.success(page);
    }

    /**
     *
     * 获取待审核广告列表
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/pending")
    public BaseResponse<Page<AdContent>> getPendingAds(@RequestParam(name="page",defaultValue = "1")int current,
                                                       @RequestParam(name="size",defaultValue = "10")int size){
        Page<AdContent> page = adContentService.getPendingApprovalAds(new Page<>(current,size));
        return ResultUtils.success(page);
    }

    /**
     *
     * 新增广告
     * @param adContent
     * @return
     */
    @PostMapping("/create")
    public BaseResponse<Boolean>createAd(@RequestBody AdContent adContent){
        boolean created = adContentService.createAd(adContent);
        if(created){
            return ResultUtils.success(true);
        }else {
            return ResultUtils.error(500,"创建广告失败");
        }
    }

    /**
     * 关键词统计查询
     * @param startDate
     * @param endDate
     * @return
     */

    @PostMapping("/keyword-statistics")
    public BaseResponse<List<Map<String,Object>>>getKeywordSearchStatistics(@RequestParam("startDate")String startDate,
                                                                            @RequestParam("endDate")String endDate){
        List<Map<String,Object>> statistics = adContentService.getKeywordSearchStatistics(startDate,endDate);
        return ResultUtils.success(statistics);
    }

    @GetMapping("total")
    public BaseResponse<Integer>getCountofAD(){
        Integer count = adContentService.getCount();
        return ResultUtils.success(count);
    }

    @GetMapping("CountOfApply")
    public BaseResponse<Integer>getCountOfApply(){
        Integer count = adContentService.getCountOfApply();
        return ResultUtils.success(count);
    }
}
